//
//  EFAWelcomPVC.m
//  efairy
//
//  Created by XiangDongMBP on 2017/9/25.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFAWelcomPVC.h"
#import "EFAWelContentVC.h"

@interface EFAWelcomPVC ()<UIPageViewControllerDataSource>
@property (nonatomic, strong) NSArray<UIImage *> *imageArray;
@end

@implementation EFAWelcomPVC
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}
- (void)initNavigationBar {
    
}
- (void)initSubViews {
    EFAWelContentVC *startingViewController = [self viewControllerAtIndex:0];
    [self setViewControllers:@[startingViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    self.dataSource = self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = ((EFAWelContentVC*)viewController).pageIndex;
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }
    index--;
    return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = ((EFAWelContentVC*)viewController).pageIndex;
    if (index == NSNotFound) {
        return nil;
    }
    index++;
    if (index == [self.imageArray count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}

#pragma mark - event response
#pragma mark - private methods
- (EFAWelContentVC *)viewControllerAtIndex:(NSUInteger)index {
    if (([self.imageArray count] == 0) || (index == [self.imageArray count])) {
        return nil;
    }
    
    // Create a new view controller and pass suitable data.
    EFAWelContentVC *pageContentVC = [self.storyboard instantiateViewControllerWithIdentifier:@"EFAWelContentVC"];
    pageContentVC.img = self.imageArray[index];
    pageContentVC.pageIndex = index;
    pageContentVC.btnHidden = (index != self.imageArray.count - 1);
    return pageContentVC;
}

#pragma mark - network request
#pragma mark - getters and setters
- (NSArray<UIImage *> *)imageArray {
    if (!_imageArray) {
        _imageArray = @[[UIImage imageNamed:@"wel_one"],
                        [UIImage imageNamed:@"wel_two"],
                        [UIImage imageNamed:@"wel_three"]];
    }
    return _imageArray;
}

@end
